/*
 * @(#)BizNodeController.java 2020年1月14日 上午11:36:58
 * Copyright 2020 zmr, Inc. All rights reserved.
 * PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.ruoyi.project.activiti.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.activiti.consts.ActivitiConstant;
import com.ruoyi.project.activiti.service.IBizNodeService;
import com.ruoyi.project.activiti.vo.ProcessNodeVo;
import org.activiti.bpmn.model.*;
import org.activiti.engine.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>File：BizNodeController.java</p>
 * <p>Title: </p>
 * <p>Description:</p>
 * <p>Copyright: Copyright (c) 2020 2020年1月14日 上午11:36:58</p>
 *
 * @author sangkf
 * @version 1.0
 */
@RestController
@RequestMapping("act/node")
public class BizNodeController extends BaseController {
    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private IBizNodeService bizNodeService;

    /**
     * 获取节点列表
     *
     * @param proDefId
     * @return
     * @author sangkf
     */
    @GetMapping("list/{proDefId}")
    public AjaxResult list(@PathVariable String proDefId) {
        List<ProcessNodeVo> list = new ArrayList<>();
        BpmnModel model = repositoryService.getBpmnModel(proDefId);
        if (model != null) {
            Collection<FlowElement> flowElements = model.getMainProcess().getFlowElements();
            for (FlowElement element : flowElements) {
                ProcessNodeVo node = new ProcessNodeVo();
                node.setNodeId(element.getId());
                node.setName(element.getName());
                if (element instanceof StartEvent) {
                    // 开始节点
                    node.setType(ActivitiConstant.NODE_TYPE_START);
                } else if (element instanceof UserTask) {
                    // 用户任务
                    node.setType(ActivitiConstant.NODE_TYPE_TASK);
                } else if (element instanceof EndEvent) {
                    // 结束
                    node.setType(ActivitiConstant.NODE_TYPE_END);
                } else {
                    // 排除其他连线或节点
                    continue;
                }
                list.add(node);
            }
        }
        return AjaxResult.success(list);
    }

    /**
     * 获取节点属性
     *
     * @param nodeId
     * @return
     * @author sangkf
     */
    @GetMapping("get/{nodeId}")
    public AjaxResult get(@PathVariable String nodeId) {
        ProcessNodeVo node = new ProcessNodeVo();
        node.setNodeId(nodeId);
        // 设置关联角色，用户，负责人
        node = bizNodeService.setAuditors(node);
        return AjaxResult.success(node);
    }

    /**
     * 修改节点属性
     *
     * @param node
     * @return
     * @author sangkf
     */
    @PostMapping("update")
    public AjaxResult update(@RequestBody ProcessNodeVo node) {
        return toAjax(bizNodeService.updateBizNode(node));
    }
}
